#include <stdio.h>
#include <AlsaAudioUtils.h>

int	main(int argc, char **argv) {
    
    static char *device = "default";
    unsigned char buffer[16*1024];

    for (int i = 0; i < sizeof(buffer); i++)
    {
        buffer[i] = random() & 0xff;
    }
    
    snd_pcm_t *handle;
    snd_pcm_open(&handle, device, SND_PCM_STREAM_PLAYBACK, 0);
    snd_pcm_set_params(handle, SND_PCM_FORMAT_U8, SND_PCM_ACCESS_RW_INTERLEAVED, 1, 48000, 1, 500000);

    snd_pcm_sframes_t frames;
    for (int i = 0; i < 16; i++)
    {
        frames = snd_pcm_writei(handle, buffer, sizeof(buffer));
        if (frames < 0) {
            frames = snd_pcm_recover(handle, frames, 0);
        }
        if (frames < 0) {
            printf("snd_pcm_writei failed: %s\n", snd_strerror(frames));
            break;
        }
        if ((frames > 0 && frames < sizeof(buffer))) {
            printf("Short write (expected %li, wrote %li)\n", sizeof(buffer), frames);
        }
    }
    
	snd_pcm_close(handle);
    return 0;
}